package com.hnas.oracle.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.core.exception.EntityNotFoundException;
import com.hnas.oracle.dao.HntEmployeeCardDAO;
import com.hnas.oracle.domain.HntEmployeeInfo;
import com.hnas.oracle.dto.DTOHntEmployeeCard;
import com.hnas.oracle.orm.HntEmployeeCard;

@Component("hntEmployeeCardService")
@Transactional(isolation = Isolation.READ_COMMITTED, readOnly = false, propagation = Propagation.REQUIRED, value = "transactionManagerHnt")
public class HntEmployeeCardServiceImpl implements HntEmployeeCardService {

	@Autowired
	private HntEmployeeCardDAO hntEmployeeCardDAO;
	
	@Override
	public List<HntEmployeeCard> getCardList(String employeeId) {
		return this.hntEmployeeCardDAO.getCardList(employeeId);
	}
	
	@Override
	public HntEmployeeCard saveHntEmployeeCard(DTOHntEmployeeCard dto) throws EntityNotFoundException {
		HntEmployeeCard vo = this.getHntEmployeeCard(dto);
		if (null == vo) {
			vo = new HntEmployeeCard();
			BeanUtils.copyProperties(dto, vo, new String[] { "id" });
			Date date = new Date();
			vo.setADD_TIME(date);
			vo.setEDIT_TIME(date);
			Date cardDate = (dto==null||dto.getCARD_DATE()==null)?date:dto.getCARD_DATE();
			vo.setCARD_DATE(cardDate);
			this.hntEmployeeCardDAO.persist(vo);
			return vo;
		} else {
			BeanUtils.copyProperties(dto, vo, new String[] { "id" });
			vo.setEDIT_TIME(new Date());
			this.hntEmployeeCardDAO.merge(vo);
			return vo;
		}
	}
	
	private HntEmployeeCard getHntEmployeeCard(DTOHntEmployeeCard dto) {
		if (null != dto.getId() && dto.getId() > 0) {
			return this.hntEmployeeCardDAO.find(dto.getId());
		}
		return null;
	}

	@Override
	public HntEmployeeInfo findEmployeeByCardNo(String cardNo) {
		return this.hntEmployeeCardDAO.findEmployeeByCardNo(cardNo);
	}
	
}